/**click
 * $Id: Actions.js,v 1.33 2012-06-30 05:47:52 gaudenz Exp $
 * Copyright (c) 2006-2012, JGraph Ltd
 */
/**
 * Constructs the actions object for the given UI.
 */
function Actions(editorUi, items, itemStyles, sqlNameTest, sqlNameValue, systemEntityText, systemEntityValue, openitem)
{



    this.editorUi = editorUi;
    this.openitem = openitem;

    this.actions = new Object();
    this.init();


}
;

/**
 * Adds the default actions.
 */

Array.prototype.remove = function() {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];
        while ((ax = this.indexOf(what)) != -1) {
            this.splice(ax, 1);
        }
    }
    return this;
}



var ui;


function ShowitemoPeration() {

    ui.showDialog(new ItemsOperationsDialog(ui, "", "").container, 580, 260, true, true);

}

Actions.prototype.init = function()
{

    ui = this.editorUi;
    var editor = ui.editor;
    var graph = editor.graph;

    var openitem = this.openitem;

    var myitems = new Array("Customer", "Region", "Fieled", "Rack", "Shelf", "Card", "CPE", "DP");
    dataupdate(myitems);


    function getEdges(id) {

        var returnData = "";
        var cells = graph.getChildCells(graph.getDefaultParent(), true, true);
        for (var i = 0; i < cells.length; i++) {
            var v = cells[i].getValue();


            if (cells[i].isEdge()) {

                if (cells[i].source.getId() == id) {

                    returnData += cells[i].target.getValue() + "__";


                }


            }
        }

        return returnData;
    }










    function getStateClassesXML()
    {

        var data = '<state-classification>';





        var cells = graph.getChildCells(graph.getDefaultParent(), true, true);
        for (var i = 0; i < cells.length; i++) {
            if (!cells[i].isEdge()) {
                var mycell = cells[i].getId().split("__");


                var addf = "";

                if (mycell[0].replaceAll("_", " ") == "Final") {

                    addf = "final='true'";


                }


                data += '<state id="' + cells[i].getValue() + '"  class="' + mycell[0].replaceAll("_", " ") + '"  ' + addf + ' />';
            }
        }



        data += '</state-classification>';

        return data;

    }
    function getDatasetOperationsXML()
    {




        var data = '<dataset-operations>';

        var dataSub = "";

        if ($(".migrateEvent").val() != null && $(".migrateEvent").val() != undefined && $(".migrateEvent").val() != "") {

            dataSub += '<migrate ' + updateAttr("event", getValueData("migrateEvent", 1)[0]) + '  ' + updateAttr("display-name", getValueData("migrateEvent", 1)[1]) + '  />';
        }

        if ($(".mer").val() != null && $(".mer").val() != undefined && $(".mer").val() != "") {

            dataSub += '<migrate-error-resolve ' + updateAttr("event", getValueData("mer", 1)[0]) + '  ' + updateAttr("display-name", getValueData("mer", 1)[1]) + ' >';

            $(".merEventdata").each(function() {
                dataSub += '<eventdata   ' + updateAttr("value", getValueDataByValue($(this).val(), 1)[0]) + '   ' + updateAttr("display-name", getValueDataByValue($(this).val(), 1)[1]) + '  />';
            })



            if ($(".mer_pattern").val() != null) {






                dataSub += '<eventdata-pattern ' + updateAttr("value", getValueDataByValue($(".mer_pattern").val(), 1)[0]) + '  ' + updateAttr("display-name", getValueDataByValue($(".mer_pattern").val(), 1)[1]) + ' />';

            }
            ;



            dataSub += '</migrate-error-resolve>';

        }




        if ($(".ser").val() != null && $(".ser").val() != undefined && $(".ser").val() != "") {


            dataSub += '<synchronise-error-resolve  ' + updateAttr("event", getValueData("ser", 1)[0]) + '  ' + updateAttr("display-name", getValueData("ser", 1)[1]) + ' >';

            $(".serEventdata").each(function() {
                dataSub += '<eventdata ' + updateAttr("value", getValueDataByValue($(this).val(), 1)[0]) + '   ' + updateAttr("display-name", getValueDataByValue($(this).val(), 1)[1]) + ' />';
            })



            if ($(".ser_pattern").val() != null) {



                dataSub += '<eventdata-pattern  ' + updateAttr("value", getValueDataByValue($(".ser_pattern").val(), 1)[0]) + '  ' + updateAttr("display-name", getValueDataByValue($(".ser_pattern").val(), 1)[1]) + '/>';
            }
            dataSub += '</synchronise-error-resolve>';

        }


        data += dataSub;

        data += '</dataset-operations>';

        if ($.trim(dataSub) == "") {
            data = "";

        }



        return data;



    }



    function getTragetValue(val) {


        var returnData = "";
        var cells = graph.getChildCells(graph.getDefaultParent(), true, true);
        for (var i = 0; i < cells.length; i++) {

            var mycell = cells[i];

            if (mycell.isEdge()) {

                if (mycell.source.getValue() == val) {


                    returnData = mycell.target.getValue();
                }

            }


        }


        return returnData;
    }




    function  getTransitionXML(val) {

        var returnData = "";
        var cells = graph.getChildCells(graph.getDefaultParent(), true, true);

        var array_ids = [];
        var array_value = [];

        var myd = 0;
        for (var i = 0; i < cells.length; i++) {

            var mycell = cells[i];

            if (mycell.isEdge()) {




                if (mycell.source.getValue() == val && array_ids.indexOf(mycell.getId()) == "-1") {


                    var targetval = mycell.target.getValue();
                    if (targetval != null && targetval != undefined && targetval != "")
                    {


                        var myi = mycell.getId();

                        array_ids.push(myi);
                        var clval = val.replaceAll(" ", "_");

                        $("." + myi + "." + clval + "__tarnsData.tarns_value").each(function() {

                            if (array_value.indexOf(mycell.target.getValue()) == "-1") {
                                array_value.push(mycell.target.getValue());

                               
                                var ev = $("." + mycell.getId() + "." + clval + "__tarnsData.tarns_value").val();
                                var adata = $("." + mycell.getId() + "." + clval + "__tarnsData.tarns").val();

                                myd++;
                                if (ev == "none") {
                                    returnData += '<transition  ' + getAttrValue(adata, "target,eventSataus") + '  ' + updateAttr("target", mycell.target.getValue()) + '    />';

                                } else {
                                    returnData += '<transition event="' + Chtml(ev) + '" ' + getAttrValue(adata, "target,eventSataus") + '  ' + updateAttr("target", mycell.target.getValue()) + '    />';


                                }





                            }

                        });


                    }

                }
            }
        }
        return returnData;



    }

    function getSCXML() {
        var returnData = '<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" xmlns:action="urn:celona-com:schema/celona/evolve/actions/0.2" initialstate="' + $(".itmedata .readyitem").val() + '">'



        var cells = graph.getChildCells(graph.getDefaultParent(), true, true);
        for (var i = 0; i < cells.length; i++) {
            if (!cells[i].isEdge()) {


                var mycell = cells[i].getId().split("__");





                returnData += '<state id="' + cells[i].getValue() + '">';







                var returnDataEntry = "<onentry>";


                var returnDataEntryData = "";
                var myv = cells[i].getValue().replaceAll(" ", "_");
                var myi = cells[i].getId();



                var xdata = $("." + myi + "." + myv + "__Data.log").val()

                if (xdata != undefined || xdata != null) {

                    returnDataEntryData += "<log " + getAttrValue(xdata) + "  />";


                }


                var sdata = $("." + myi + "." + myv + "__Data.send").val();
                if (sdata != undefined || sdata != null) {

                    returnDataEntryData += "<send " + getAttrValue(sdata) + "  />";


                }

                var adata = $("." + myi + "." + myv + "__Data.action").val();




                if (adata != undefined || adata != null) {
                    var myval = getValueDataByValue(adata, 1);



                    returnDataEntryData += "<action:" + myval[0] + " " + getAttrValue(adata, "data_action") + "  />";


                }



                returnDataEntry += returnDataEntryData;


                returnDataEntry += "</onentry>";

                if ($.trim(returnDataEntryData) == "") {
                    returnDataEntry = "";
                }

                returnData += returnDataEntry;






                var returnDataExit = "<onexit>";
                var returnDataExitData = "";


                myv = cells[i].getValue().replaceAll(" ", "_");

                var myi = cells[i].getId();


                xdata = $("." + myi + "." + myv + "__Data.Onexit_log").val()

                if (xdata != undefined || xdata != null) {

                    returnDataExitData += "<log " + getAttrValue(xdata) + "  />";


                }


                sdata = $("." + myi + "." + myv + "__Data.Onexit_send").val();
                if (sdata != undefined || sdata != null) {

                    returnDataExitData += "<send " + getAttrValue(sdata) + "  />";


                }

                adata = $("." + myi + "." + myv + "__Data.Onexit_action").val();




                if (adata != undefined || adata != null) {
                    myval = getValueDataByValue(adata, 1);

                    returnDataExitData += "<action:" + myval[0] + " " + getAttrValue(adata, "data_action") + "  />";


                }




                returnDataExit += returnDataExitData;

                returnDataExit += "</onexit>";


                if ($.trim(returnDataExitData) == "") {
                    returnDataExit = "";
                }


                returnData += returnDataExit;




                returnData += getTransitionXML(cells[i].getValue());





                if ($("." + myv + "__Data.targetless_transititon").val() != undefined && $("." + myv + "__Data.targetless_transititon").val() != null) {

                    returnData += $("." + myv + "__Data.targetless_transititon").val();

                }





                returnData += '</state>';

            }
        }









        returnData += '</scxml>';
        return  returnData;

    }




    function getDirectionXml() {






        setSesstion("stat_modelName", Chtml($(".modleName").val()), function() {

            var value = getDatasetOperationsXML();
            value += getStateClassesXML();
            value += getSCXML();

            setSesstion("stat", value, function() {
                showXmlFile();


            });
        });


        return value;

    }





    graph.addListener(mxEvent.CELLS_ADDED, function(sender, evt)
    {




        var cell = evt.getProperty("cells");

        if (cell[0].isEdge()) {



            //  graph.getModel().setStyle(cell[0], "edgeStyle=entityRelationEdgeStyle");



            if (cell[0].target != null && cell[0].target != undefined) {


                getSesstion("itemsOperations_Names", function(data) {


                    ui.showDialog(new UpdateItemEdgeDialog(ui, cell[0].source.getValue(), cell[0].target.getValue(), data, cell[0]).container, 420, 230, true, true);



                })




            } else {

                graph.cellsRemoved([cell[0]]);

            }


        } else {





            if (cell[0].getValue() == "ready" || cell[0].getValue() == "Ready") {


                $(function() {

                    var data = "<input type='hidden' class='readyitem'  value='" + cell[0].getValue() + "' />";


                    $(".itmedata").append(data);

                })





                $(".geSidebar .geItem").eq(0).hide();

            }


            var myval = cell[0].getValue().replaceAll(" ", "_") + "__" + cell[0].getId();

            cell[0].setId(myval);




        }

    })





    $(".cancelitem").click(function() {


        $(".itemWindow").hide();


    })



    $(function() {



        ui.showDialog(new modUpdateItemDialog(ui, mxUtils).container, 420, 230, true, true);





    })










    var nameValue = "Ready";
    var namesttyle = 'shape=label;fillColor=#BFBFBF;strokeColor=#000000;';


    var v1 = graph.insertVertex(graph.getDefaultParent(), null, nameValue, 100, 50, 120, 60, namesttyle);

    addEditEvent(graph, v1, nameValue, this.editorUi);

    $(function() {

        $(".geSidebar .geItem").eq(0).hide();

    })



    graph.addListener(mxEvent.DOUBLE_CLICK, function(sender, evt)
    {

        var classCell = evt.getProperty("cell");

        if (classCell.isEdge()) {

            getSesstion("itemsOperations_Names", function(data) {

                ui.showDialog(new UpdateItemEdgeDialog(ui, classCell.source.getValue(), classCell.target.getValue(), data, classCell).container, 420, 230, true, true);


            })




        }
        else {





            var tpe = $(".itmedata .readyitem").val();



            if (tpe == classCell.getValue()) {

                var data = classCell.getValue();
                $(".dataitem").val(data);
                $(".datalabel").html(data);


                ui.showDialog(new updateItemDialog(ui, data, classCell, graph).container, 300, 80, true, true);



            } else {

                var data = classCell.getValue();
                $(".dataitem").val(data);
                $(".datalabel").html(data);


                ui.showDialog(new UpdateItemDialog(ui, graph, classCell).container, 620, 430, true, true);



            }

        }





    })


    graph.addListener(mxEvent.CELLS_REMOVED, function(sender, evt)
    {


        var cell = evt.getProperty("cells");
        if (!cell[0].isEdge()) {




            var val = cell[0].getValue();


            if (val == "ready" || val == "Ready") {

                $(function() {



                    $(".itmedata " + " ." + cell[0].getId()).remove();
                })


                $(".geSidebar .geItem").show();

            }



            $(function() {
                val = val.split(" ");


                myitems.push($.trim(val[0]))
                $(".itmedata ." + $.trim(val[0]) + "_data").remove();



                //  rearray();



            });
        } else {





        }

    })












    this.addAction('modelData', function() {

    });



    this.addAction('new', function() {
        window.open(ui.getUrl());
    });
    this.addAction('open', function()
    {


        window.openNew = true;
        window.openKey = 'open';

        ui.openFile();
    });






    this.addAction('import', function()
    {



    });



    this.addAction('save', function() {
        ui.saveFile(false);
    }, null, null, 'Ctrl+S');
    this.addAction('saveAs', function() {
        ui.saveFile(true);
    }, null, null, 'Ctrl+Shift-S');
    this.addAction('export', function() {
        ui.showDialog(new ExportDialog(ui).container, 300, 200, true, true);
    }, null, null, 'Ctrl+E');
    this.put('editFile', new Action(mxResources.get('showXML'), mxUtils.bind(this, function()
    {


        getDirectionXml();

    })));
    this.addAction('pageSetup', function() {



        ui.showDialog(new modUpdateItemDialog(ui, mxUtils).container, 420, 230, true, true);

    });
    this.addAction('print', function() {
        ui.showDialog(new PrintDialog(ui).container, 300, 200, true, true);
    }, null, 'sprite-print', 'Ctrl+P');
    this.addAction('preview', function() {
        mxUtils.show(graph, null, 10, 10);
    });

    // Edit actions
    this.addAction('undo', function() {
        editor.undoManager.undo();
    }, null, 'sprite-undo', 'Ctrl+Z');
    this.addAction('redo', function() {
        editor.undoManager.redo();
    }, null, 'sprite-redo', 'Ctrl+Y');
    this.addAction('cut', function() {
        mxClipboard.cut(graph);
    }, null, 'sprite-cut', 'Ctrl+X');
    this.addAction('copy', function() {
        mxClipboard.copy(graph);
    }, null, 'sprite-copy', 'Ctrl+C');
    this.addAction('paste', function() {
        mxClipboard.paste(graph);
    }, false, 'sprite-paste', 'Ctrl+V');
    this.addAction('delete', function() {
        graph.removeCells();
    }, null, null, 'Delete');
    this.addAction('duplicate', function()
    {
        var s = graph.gridSize;
        graph.setSelectionCells(graph.moveCells(graph.getSelectionCells(), s, s, true));
    }, null, null, 'Ctrl+D');
    this.addAction('selectVertices', function() {
        graph.selectVertices();
    }, null, null, 'Ctrl+Shift+V');
    this.addAction('selectEdges', function() {
        graph.selectEdges();
    }, null, null, 'Ctrl+Shift+E');
    this.addAction('selectAll', function() {
        graph.selectAll();
    }, null, null, 'Ctrl+A');

    // Navigation actions
    this.addAction('home', function() {
        graph.home();
    }, null, null, 'Home');
    this.addAction('exitGroup', function() {
        graph.exitGroup();
    }, null, null, 'Page Up');
    this.addAction('enterGroup', function() {
        graph.enterGroup();
    }, null, null, 'Page Down');
    this.addAction('expand', function() {
        graph.foldCells(false);
    }, null, null, 'Enter');
    this.addAction('collapse', function() {
        graph.foldCells(true);
    }, null, null, 'Backspace');

    // Arrange actions
    this.addAction('toFront', function() {
        graph.orderCells(false);
    }, null, null, 'Ctrl+F');
    this.addAction('toBack', function() {
        graph.orderCells(true);
    }, null, null, 'Ctrl+B');
    this.addAction('group', function() {
        graph.setSelectionCell(graph.groupCells(null, 0));
    }, null, null, 'Ctrl+G');
    this.addAction('ungroup', function() {
        graph.setSelectionCells(graph.ungroupCells());
    }, null, null, 'Ctrl+U');
    this.addAction('removeFromGroup', function() {
        graph.removeCellsFromParent();
    });
    this.addAction('autosize', function()
    {
        var cells = graph.getSelectionCells();

        if (cells != null)
        {
            graph.getModel().beginUpdate();
            try
            {
                for (var i = 0; i < cells.length; i++)
                {
                    var cell = cells[i];

                    if (graph.getModel().getChildCount(cell))
                    {
                        graph.updateGroupBounds([cell], 20);
                    }
                    else
                    {
                        graph.updateCellSize(cell);
                    }
                }
            }
            finally
            {
                graph.getModel().endUpdate();
            }
        }
    });
    this.addAction('rotation', function()
    {
        var value = '0';
        var state = graph.getView().getState(graph.getSelectionCell());

        if (state != null)
        {
            value = state.style[mxConstants.STYLE_ROTATION] || value;
        }

        value = mxUtils.prompt(mxResources.get('enterValue') + ' (' +
                mxResources.get('rotation') + ' 0-360)', value);

        if (value != null)
        {
            graph.setCellStyles(mxConstants.STYLE_ROTATION, value);
        }
    });
    this.addAction('rotate', function()
    {
        var cells = graph.getSelectionCells();

        if (cells != null)
        {
            graph.getModel().beginUpdate();
            try
            {
                for (var i = 0; i < cells.length; i++)
                {
                    var cell = cells[i];

                    if (graph.getModel().isVertex(cell) && graph.getModel().getChildCount(cell) == 0)
                    {
                        var geo = graph.getCellGeometry(cell);

                        if (geo != null)
                        {
                            // Rotates the size and position in the geometry
                            geo = geo.clone();
                            geo.x += geo.width / 2 - geo.height / 2;
                            geo.y += geo.height / 2 - geo.width / 2;
                            var tmp = geo.width;
                            geo.width = geo.height;
                            geo.height = tmp;
                            graph.getModel().setGeometry(cell, geo);

                            // Reads the current direction and advances by 90 degrees
                            var state = graph.view.getState(cell);

                            if (state != null)
                            {
                                var dir = state.style[mxConstants.STYLE_DIRECTION] || 'east'/*default*/;

                                if (dir == 'east')
                                {
                                    dir = 'south';
                                }
                                else if (dir == 'south')
                                {
                                    dir = 'west';
                                }
                                else if (dir == 'west')
                                {
                                    dir = 'north';
                                }
                                else if (dir == 'north')
                                {
                                    dir = 'east';
                                }

                                graph.setCellStyles(mxConstants.STYLE_DIRECTION, dir, [cell]);
                            }
                        }
                    }
                }
            }
            finally
            {
                graph.getModel().endUpdate();
            }
        }
    }, null, null, 'Ctrl+R');

    // View actions
    this.addAction('actualSize', function()
    {
        graph.zoomTo(1);
    });
    this.addAction('zoomIn', function() {
        graph.zoomIn();
    }, null, null, 'Add');
    this.addAction('zoomOut', function() {
        graph.zoomOut();
    }, null, null, 'Subtract');
    this.addAction('fitWindow', function() {
        graph.fit();
    });

    this.addAction('fitPage', mxUtils.bind(this, function()
    {
        if (!graph.pageVisible)
        {
            this.get('pageView').funct();
        }

        var fmt = graph.pageFormat;
        var ps = graph.pageScale;
        var cw = graph.container.clientWidth - 20;
        var ch = graph.container.clientHeight - 20;

        var scale = Math.floor(100 * Math.min(cw / fmt.width / ps, ch / fmt.height / ps)) / 100;
        graph.zoomTo(scale);

        graph.container.scrollLeft = Math.round(graph.view.translate.x * scale - Math.max(10, (graph.container.clientWidth - fmt.width * ps * scale) / 2));
        graph.container.scrollTop = Math.round(graph.view.translate.y * scale - Math.max(10, (graph.container.clientHeight - fmt.height * ps * scale) / 2));
    }));
    this.addAction('fitPageWidth', mxUtils.bind(this, function()
    {
        if (!graph.pageVisible)
        {
            this.get('pageView').funct();
        }

        var fmt = graph.pageFormat;
        var ps = graph.pageScale;
        var cw = graph.container.clientWidth - 20;

        var scale = Math.floor(100 * cw / fmt.width / ps) / 100;
        graph.zoomTo(scale);

        graph.container.scrollLeft = Math.round(graph.view.translate.x * scale - Math.max(10, (graph.container.clientWidth - fmt.width * ps * scale) / 2));
        graph.container.scrollTop = Math.round(graph.view.translate.y * scale - Math.max(10, (graph.container.clientHeight - fmt.height * ps * scale) / 2));
    }));
    this.put('customZoom', new Action(mxResources.get('custom'), function()
    {
        var value = mxUtils.prompt(mxResources.get('enterValue') + ' (%)', parseInt(graph.getView().getScale() * 100));

        if (value != null && value.length > 0 && !isNaN(parseInt(value)))
        {
            graph.zoomTo(parseInt(value) / 100);
        }
    }));

    // Option actions
    var action = null;
    action = this.addAction('grid', function()
    {
        graph.setGridEnabled(!graph.isGridEnabled());
        editor.updateGraphComponents();
    }, null, null, 'Ctrl+Shift+G');
    action.setToggleAction(true);
    action.setSelectedCallback(function() {
        return graph.isGridEnabled();
    });
    action = this.addAction('guides', function() {
        graph.graphHandler.guidesEnabled = !graph.graphHandler.guidesEnabled;
    });
    action.setToggleAction(true);
    action.setSelectedCallback(function() {
        return graph.graphHandler.guidesEnabled;
    });
    action = this.addAction('tooltips', function()
    {
        graph.tooltipHandler.setEnabled(!graph.tooltipHandler.isEnabled());
    });
    action.setToggleAction(true);
    action.setSelectedCallback(function() {
        return graph.tooltipHandler.isEnabled();
    });
    action = this.addAction('navigation', function()
    {
        graph.foldingEnabled = !graph.foldingEnabled;
        graph.view.revalidate();
    });
    action.setToggleAction(true);
    action.setSelectedCallback(function() {
        return graph.foldingEnabled;
    });
    action = this.addAction('scrollbars', function()
    {
        graph.scrollbars = !graph.scrollbars;
        editor.updateGraphComponents();

        if (!graph.scrollbars)
        {
            var t = graph.view.translate;
            graph.view.setTranslate(t.x - graph.container.scrollLeft / graph.view.scale, t.y - graph.container.scrollTop / graph.view.scale);
            graph.container.scrollLeft = 0;
            graph.container.scrollTop = 0;
            graph.sizeDidChange();
        }
        else
        {
            var dx = graph.view.translate.x;
            var dy = graph.view.translate.y;

            graph.view.translate.x = 0;
            graph.view.translate.y = 0;
            graph.sizeDidChange();
            graph.container.scrollLeft -= Math.round(dx * graph.view.scale);
            graph.container.scrollTop -= Math.round(dy * graph.view.scale);
        }
    }, !mxClient.IS_TOUCH);
    action.setToggleAction(true);
    action.setSelectedCallback(function() {
        return graph.container.style.overflow == 'auto';
    });
    action = this.addAction('pageView', mxUtils.bind(this, function()
    {
        graph.pageVisible = !graph.pageVisible;
        graph.pageBreaksVisible = graph.pageVisible;
        graph.preferPageSize = graph.pageBreaksVisible;
        graph.view.validate();
        graph.sizeDidChange();

        editor.updateGraphComponents();
        editor.outline.update();

        if (mxUtils.hasScrollbars(graph.container))
        {
            if (graph.pageVisible)
            {
                graph.container.scrollLeft -= 20;
                graph.container.scrollTop -= 20;
            }
            else
            {
                graph.container.scrollLeft += 20;
                graph.container.scrollTop += 20;
            }
        }
    }));
    action.setToggleAction(true);
    action.setSelectedCallback(function() {
        return graph.pageVisible;
    });
    this.put('pageBackgroundColor', new Action(mxResources.get('backgroundColor'), function()
    {
        var apply = function(color)
        {
            graph.background = color;
            editor.updateGraphComponents();
        };

        var cd = new ColorDialog(ui, graph.background || 'none', apply);
        ui.showDialog(cd.container, 220, 360, true, false);

        if (!mxClient.IS_TOUCH)
        {
            cd.colorInput.focus();
        }
    }));
    action = this.addAction('connect', function()
    {
        graph.setConnectable(!graph.connectionHandler.isEnabled());
    }, null, null, 'Ctrl+Q');
    action.setToggleAction(true);
    action.setSelectedCallback(function() {
        return graph.connectionHandler.isEnabled();
    });

    // Help actions
    this.addAction('help', function()
    {
        var ext = '';

        if (mxResources.isLanguageSupported(mxClient.language))
        {
            ext = '_' + mxClient.language;
        }

        window.open(RESOURCES_PATH + '/help' + ext + '.html');
    });
    this.put('about', new Action(mxResources.get('about') + ' Graph Editor', function()
    {
        ui.showDialog(new AboutDialog(ui).container, 320, 280, true, true);
    }, null, null, 'F1'));

    // Font style actions
    var toggleFontStyle = mxUtils.bind(this, function(key, style)
    {
        this.addAction(key, function()
        {
            graph.toggleCellStyleFlags(mxConstants.STYLE_FONTSTYLE, style);
        });
    });

    toggleFontStyle('bold', mxConstants.FONT_BOLD);
    toggleFontStyle('italic', mxConstants.FONT_ITALIC);
    toggleFontStyle('underline', mxConstants.FONT_UNDERLINE);

    // Color actions
    this.addAction('fontColor', function() {
        ui.menus.pickColor(mxConstants.STYLE_FONTCOLOR);
    });
    this.addAction('strokeColor', function() {
        ui.menus.pickColor(mxConstants.STYLE_STROKECOLOR);
    });
    this.addAction('fillColor', function() {
        ui.menus.pickColor(mxConstants.STYLE_FILLCOLOR);
    });
    this.addAction('gradientColor', function() {
        ui.menus.pickColor(mxConstants.STYLE_GRADIENTCOLOR);
    });
    this.addAction('backgroundColor', function() {
        ui.menus.pickColor(mxConstants.STYLE_LABEL_BACKGROUNDCOLOR);
    });
    this.addAction('borderColor', function() {
        ui.menus.pickColor(mxConstants.STYLE_LABEL_BORDERCOLOR);
    });

    // Format actions
    this.addAction('shadow', function() {
        graph.toggleCellStyles(mxConstants.STYLE_SHADOW);
    });
    this.addAction('dashed', function() {
        graph.toggleCellStyles(mxConstants.STYLE_DASHED);
    });
    this.addAction('rounded', function() {
        graph.toggleCellStyles(mxConstants.STYLE_ROUNDED);
    });
    this.addAction('style', function()
    {
        var cells = graph.getSelectionCells();

        if (cells != null && cells.length > 0)
        {
            var model = graph.getModel();
            var style = mxUtils.prompt(mxResources.get('enterValue') + ' (' + mxResources.get('style') + ')',
                    model.getStyle(cells[0]) || '');

            if (style != null)
            {
                graph.setCellStyle(style, cells);
            }
        }
    });
    this.addAction('setAsDefaultEdge', function()
    {
        var cell = graph.getSelectionCell();

        if (cell != null && graph.getModel().isEdge(cell))
        {
            // Take a snapshot of the cell at the moment of calling
            var proto = graph.getModel().cloneCells([cell])[0];

            // Uses edge template for connect preview
            graph.connectionHandler.createEdgeState = function(me)
            {
                return graph.view.createState(proto);
            };

            // Creates new connections from edge template
            graph.connectionHandler.factoryMethod = function()
            {
                return graph.cloneCells([proto])[0];
            };
        }
    });
    this.addAction('image', function()
    {
        function updateImage(value, w, h)
        {
            var select = null;
            var cells = graph.getSelectionCells();

            graph.getModel().beginUpdate();
            try
            {
                // Inserts new cell if no cell is selected
                if (cells.length == 0)
                {
                    var gs = graph.getGridSize();
                    cells = [graph.insertVertex(graph.getDefaultParent(), null, '', gs, gs, w, h)];
                    select = cells;
                }

                graph.setCellStyles(mxConstants.STYLE_IMAGE, value, cells);
                graph.setCellStyles(mxConstants.STYLE_SHAPE, 'image', cells);

                if (graph.getSelectionCount() == 1)
                {
                    if (w != null && h != null)
                    {
                        var cell = cells[0];
                        var geo = graph.getModel().getGeometry(cell);

                        if (geo != null)
                        {
                            geo = geo.clone();
                            geo.width = w;
                            geo.height = h;
                            graph.getModel().setGeometry(cell, geo);
                        }
                    }
                }
            }
            finally
            {
                graph.getModel().endUpdate();
            }

            if (select != null)
            {
                graph.setSelectionCells(select);
                graph.scrollCellToVisible(select[0]);
            }
        }
        ;

        var value = '';
        var state = graph.getView().getState(graph.getSelectionCell());

        if (state != null)
        {
            value = state.style[mxConstants.STYLE_IMAGE] || value;
        }

        value = mxUtils.prompt(mxResources.get('enterValue') + ' (' + mxResources.get('url') + ')', value);

        if (value != null)
        {
            if (value.length > 0)
            {
                var img = new Image();

                img.onload = function()
                {
                    updateImage(value, img.width, img.height);
                };
                img.onerror = function()
                {
                    mxUtils.alert(mxResources.get('fileNotFound'));
                };

                img.src = value;
            }
        }
    });
};

/**
 * Registers the given action under the given name.
 */
Actions.prototype.addAction = function(key, funct, enabled, iconCls, shortcut)
{
    return this.put(key, new Action(mxResources.get(key), funct, enabled, iconCls, shortcut));
};

/**
 * Registers the given action under the given name.
 */
Actions.prototype.put = function(name, action)
{
    this.actions[name] = action;

    return action;
};

/**
 * Returns the action for the given name or null if no such action exists.
 */
Actions.prototype.get = function(name)
{
    return this.actions[name];
};

/**
 * Constructs a new action for the given parameters.
 */
function Action(label, funct, enabled, iconCls, shortcut)
{
    mxEventSource.call(this);
    this.label = label;
    this.funct = funct;
    this.enabled = (enabled != null) ? enabled : true;
    this.iconCls = iconCls;
    this.shortcut = shortcut;
}
;

// Action inherits from mxEventSource
mxUtils.extend(Action, mxEventSource);

/**
 * Sets the enabled state of the action and fires a stateChanged event.
 */
Action.prototype.setEnabled = function(value)
{
    if (this.enabled != value)
    {
        this.enabled = value;
        this.fireEvent(new mxEventObject('stateChanged'));
    }
};

/**
 * Sets the enabled state of the action and fires a stateChanged event.
 */
Action.prototype.setToggleAction = function(value)
{
    this.toggleAction = value;
};

/**
 * Sets the enabled state of the action and fires a stateChanged event.
 */
Action.prototype.setSelectedCallback = function(funct)
{
    this.selectedCallback = funct;
};

/**
 * Sets the enabled state of the action and fires a stateChanged event.
 */
Action.prototype.isSelected = function()
{
    return this.selectedCallback();
};
